package com.yss.reportworld.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.constant.RequestDataHelper;
import com.yss.common.core.enums.TableVersionEnum;
import com.yss.common.core.enums.YssStatusEnum;
import com.yss.common.core.exception.BusinessException;
import com.yss.common.security.utils.DictUtils;
import com.yss.reportworld.domain.kettle.EtlJobDetail;
import com.yss.reportworld.domain.kettle.EtlJobTaskDetail;
import com.yss.reportworld.enums.ReportWorldEnum;
import com.yss.reportworld.mapper.kettle.EtlJobDetailMapper;
import com.yss.reportworld.mapper.kettle.EtlJobTaskDetailMapper;
import com.yss.reportworld.util.TableInfoUtil;
import com.yss.system.api.domain.SysDictData;
import lombok.extern.slf4j.Slf4j;
import com.yss.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yss.reportworld.service.IEtlJobTaskService;
import com.yss.reportworld.mapper.kettle.EtlJobTaskMapper;
import com.yss.reportworld.domain.kettle.EtlJobTask;

/**
 * 采集任务Service业务层处理
 *
 * @author yss
 * @date 2023-07-14
 */
@Slf4j
@Service
public class EtlJobTaskServiceImpl extends ServiceImpl<EtlJobTaskMapper, EtlJobTask> implements IEtlJobTaskService {
    @Autowired
    private EtlJobTaskMapper etlJobTaskMapper;
    @Autowired
    private EtlJobTaskDetailMapper etlJobTaskDetailMapper;
    @Autowired
    private EtlJobDetailMapper etlJobDetailMapper;

    /**
     * 查询采集任务列表
     *
     * @param etlJobTask 采集任务
     * @return 采集任务
     */
    @Override
    public List<EtlJobTask> selectEtlJobTaskList(EtlJobTask etlJobTask) {
        try {
            QueryWrapper<EtlJobTask> wrapper = new QueryWrapper();
            wrapper.orderByAsc("T1.ORDER_NUM");
            wrapper.eq("T1.STATUS", YssStatusEnum.NORMAL.getCode());
            if (StringUtils.isNull(etlJobTask)) {
                return new ArrayList<>();
            }
            TableInfoUtil.handleTableVersion(etlJobTask.getHisVersion());
            if (StringUtils.isNotEmpty(etlJobTask.getTaskName())) {
                wrapper.like("T1.TASK_NAME", etlJobTask.getTaskName());
            }
            if (StringUtils.isNotEmpty(etlJobTask.getRemark())) {
                wrapper.like("T1.REMARK", etlJobTask.getRemark());
            }
            List<EtlJobTask> listJobTabk = etlJobTaskMapper.queryEtlJobTask(wrapper);
            if (StringUtils.isEmpty(listJobTabk)) {
                return listJobTabk;
            }
            List<EtlJobTask> listJobTabkNew = new ArrayList<>();
            for (EtlJobTask jobTask : listJobTabk) {
                if (StringUtils.isNull(jobTask) || StringUtils.isEmpty(jobTask.getId())) {
                    continue;
                }
                // 任务作业关联
                QueryWrapper<EtlJobTaskDetail> taskDetailQueryWrapper = new QueryWrapper<>();
                taskDetailQueryWrapper.eq("T1.TASK_ID", jobTask.getId());
                taskDetailQueryWrapper.orderByAsc("T1.ORDER_NUM");
                taskDetailQueryWrapper.eq("T1.RW_DB_STATUS", YssStatusEnum.NORMAL.getCode());
                List<EtlJobTaskDetail> listTaskDetail = etlJobTaskDetailMapper.queryEtlJobTaskDetail(taskDetailQueryWrapper);
                if (StringUtils.isEmpty(listTaskDetail)) {
                    continue;
                }
                //获取所有的采集作业的Id
                List<String> listJobId = listTaskDetail.stream()
                        .map(e -> new String(e.getJobId()))
                        .collect(Collectors.toList());
                if (StringUtils.isEmpty(listJobId)) {
                    continue;
                }
                // 采集作业明细
                QueryWrapper<EtlJobDetail> etlJobDetailQueryWrapper = new QueryWrapper<>();
                etlJobDetailQueryWrapper.in("T1.JOB_ID", listJobId);
                etlJobDetailQueryWrapper.eq("T1.RW_DB_DATABASE_TYPE", etlJobTask.getDataBaseType());
                etlJobDetailQueryWrapper.orderByAsc("T1.JOB_ID,T1.ORDER_NUM");
                etlJobDetailQueryWrapper.eq("T1.RW_DB_STATUS", YssStatusEnum.NORMAL.getCode());
                List<EtlJobDetail> listJobDetail = etlJobDetailMapper.queryEtlJobDetail(etlJobDetailQueryWrapper);
                if (StringUtils.isEmpty(listJobDetail)) {
                    continue;
                }
                listJobTabkNew.add(jobTask);
            }
            return listJobTabkNew;
        } catch (Exception ex) {
            throw new BusinessException("获取ETL采集任务列表失败，请稍后重试！" + ex.getMessage());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }
}
